﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using Pipra.Math;
using Pipra.Math.Geometry;

namespace Pipra.Gis.Projection
{
    /// <summary>
    /// A Cassini Soldner projection.
    /// </summary>
    public class CassiniSoldner : ProjectionBase
    {
        protected readonly LatLon NaturalOrigin;
        protected readonly double MLineCoef1;
        protected readonly double MLineCoef2;
        protected readonly double MLineCoef3;
        protected readonly double MLineCoef4;
        protected readonly double MLineCoef1Major;
        protected readonly double LatLineCoef1;
        protected readonly double LatLineCoef2;
        protected readonly double LatLineCoef3;
        protected readonly double LatLineCoef4;
        protected readonly double MOrigin;
        protected readonly double MOriginYOffset;
        protected readonly double OneMinusESq;
        protected readonly double ESecSq;
        protected readonly double OneMinusESqSqrt;

        private class Inverted : InvertedProjectionBase<CassiniSoldner>
        {

            public Inverted(CassiniSoldner core) : base(core)
            {
                if(
                    0 == Core.MLineCoef1Major
                    || 0 == Core.MajorAxis
                    || 0 == Core.OneMinusESqSqrt
                )
                {
                    throw new ArgumentException("Core cannot be inverted.");
                }
            }

            public override LatLon TransformValue(Point2 coord)
            {
                double latp = (Core.MOriginYOffset + coord.Y) / Core.MLineCoef1Major;
                latp = (
                    latp
                    + (Core.LatLineCoef1 * System.Math.Sin(2.0 * latp))
                    + (Core.LatLineCoef2 * System.Math.Sin(4.0 * latp))
                    + (Core.LatLineCoef3 * System.Math.Sin(6.0 * latp))
                    + (Core.LatLineCoef4 * System.Math.Sin(8.0 * latp))
                );
                double tanLatp = System.Math.Tan(latp);
                double t = tanLatp * tanLatp;
                double lesq = System.Math.Sin(latp); lesq = 1.0 - (lesq * lesq * Core.ESq);
                double d = ((coord.X - Core.FalseProjectedOffset.X) * System.Math.Sqrt(lesq)) / Core.MajorAxis;
                double d2 = d * d;
                double s = (1.0 + (3.0 * t)) * d2;
                return new LatLon(
                    latp - (
                        ((lesq * tanLatp) / Core.OneMinusESqSqrt)
                        * d2 * (
                            0.5
                            - (s / 24.0)
                        )
                    ),
                    Core.NaturalOrigin.Lon + (
                        (
                            d
                            - (
                                t
                                * d2 * d * (
                                    (1.0 / 3.0)
                                    + (s / 15.0)
                                )
                            )
                        )
                        / System.Math.Cos(latp)
                    )
                );
            }

        }

        /// <summary>
        /// Constructs a new cassini soldner projection.
        /// </summary>
        /// <param name="naturalOrigin">The natural origin of the projection.</param>
        /// <param name="falseProjectedOffset">The false projected offset.</param>
        /// <param name="spheroid">The spheroid.</param>
        public CassiniSoldner(
            LatLon naturalOrigin,
            ICoordinatePair<double> falseProjectedOffset,
            ISpheroid<double> spheroid
        )
            : base(falseProjectedOffset, spheroid)
        {
            NaturalOrigin = naturalOrigin;

            double e4 = ESq * ESq;
            double e6 = e4 * ESq;
            MLineCoef1 = 1.0 - (ESq / 4.0) - (3.0 / 64.0 * e4) - (5.0 / 256.0 * e6);
            MLineCoef1Major = MLineCoef1 * MajorAxis;
            MLineCoef2 = (3.0 / 8.0 * ESq) - (3.0 / 32.0 * e4) - (45.0 / 1024.0 * e6);
            MLineCoef3 = (15.0 / 256.0 * e4) + (45.0 / 1024.0 * e6);
            MLineCoef4 = (35.0 / 3072.0 * e6);
            MOrigin = MajorAxis * (
                (MLineCoef1 * naturalOrigin.Lat)
                - (MLineCoef2 * System.Math.Sin(2.0 * naturalOrigin.Lat))
                + (MLineCoef3 * System.Math.Sin(4.0 * naturalOrigin.Lat))
                - (MLineCoef4 * System.Math.Sin(6.0 * naturalOrigin.Lat))
            );
            MOriginYOffset = MOrigin - falseProjectedOffset.Y;
            OneMinusESq = (1.0 - ESq);
            ESecSq = spheroid.ESecondSquared;
            OneMinusESqSqrt = System.Math.Sqrt(OneMinusESq);
            double ep = (1.0 - OneMinusESqSqrt) / (1.0 + OneMinusESqSqrt);
            double ep2 = ep * ep;
            double ep3 = ep2 * ep;
            double ep4 = ep3 * ep;
            LatLineCoef1 = (3.0 / 2.0 * ep) - (27.0 / 32.0 * ep3);
            LatLineCoef2 = (21.0 / 16.0 * ep2) - (55.0 / 32.0 * ep4);
            LatLineCoef3 = (151.0 / 96.0 * ep3) - (1097.0 / 512.0 * ep4);
            LatLineCoef4 = (1097 / 512 * ep4);
        }

        public override Point2 TransformValue(LatLon coord)
        {
            double v = System.Math.Sin(coord.Lat);
            v = MajorAxis / System.Math.Sqrt(1.0 - (ESq * v * v));
            double c = System.Math.Cos(coord.Lat);
            double a = (coord.Lon - NaturalOrigin.Lon) * c;
            c = ESecSq * c * c;
            double tanLat = System.Math.Tan(coord.Lat);
            double t = tanLat * tanLat;
            double a2 = a * a;
            return new Point2(
                FalseProjectedOffset.X + (
                    v * (
                        a
                        - (
                            a2 * a * t * (
                                (1.0 / 6.0)
                                + (
                                    (
                                        8.0
                                        - t
                                        + (8.0 * c)
                                    )
                                    * a2
                                    / 120.0
                                )
                            )
                        )
                    )
                ),
                FalseProjectedOffset.Y + (
                    (
                        MajorAxis * (
                            (MLineCoef1 * coord.Lat)
                            - (MLineCoef2 * System.Math.Sin(2.0 * coord.Lat))
                            + (MLineCoef3 * System.Math.Sin(4.0 * coord.Lat))
                            - (MLineCoef4 * System.Math.Sin(6.0 * coord.Lat))
                        )
                    )
                    - MOrigin + (
                        v * tanLat * a2 * (
                            0.5
                            + (
                                (
                                    5
                                    - t
                                    + (6.0 * c)
                                )
                                * a2
                                / 24.0
                            )
                        )
                    )
                )
            );
        }

        public override ITransformation<Point2, LatLon> GetInverse()
        {
            return new Inverted(this);
        }

        public override bool HasInverse
        {
            get
            {
                return 0 != MLineCoef1Major
                    && 0 != MajorAxis
                    && 0 != OneMinusESqSqrt;
                ;
            }
        }

        public override string Name
        {
            get { return "Cassini Soldner"; }
        }

    }
}
